package com.jf.utils;

import com.jf.hb.HB;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.net.URL;
import java.util.Iterator;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-08-07
 * Time:2:56
 */
public class FileUtil {
    public static boolean createFile(File file) {
        boolean result = false;
        if (!file.exists()) {
            result = file.getParentFile().mkdirs();
            try {
                if (result)
                    result = file.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return result;
    }

    public static String readPropertyValues(String path, String name) {
        //InputStream inputStream = HB.class.getClassLoader().getResourceAsStream(path);
        Properties properties = new Properties();
        try {
            InputStream inputStream = new FileInputStream("./conf/login.properties");
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            properties.load(reader);
            String value = properties.getProperty(name);
            reader.close();
            return value;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void writePropertyValues(String path, String... nameValues) {
        Properties properties = new Properties();
        path = "./conf/login.properties";
        try {
            OutputStream outputStream = new FileOutputStream(path);
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            if (nameValues != null && (nameValues.length % 2) == 0) {
                for (int i = 0; i < nameValues.length; i += 2) {
                    properties.setProperty(nameValues[i], nameValues[i + 1]);
                }
            }
            properties.store(bufferedWriter, null);
            bufferedWriter.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static String readXMLBlessing(String path, int id, String element) {
        //InputStream inputStream = FileUtil.class.getClassLoader().getResourceAsStream(path);
        //String configUrl = path;
        SAXReader saxReader = null;
        try {
            InputStream inputStream = new FileInputStream("./conf/config.xml");
            //if (inputStream == null) {
            //    inputStream = new FileInputStream("./conf/config.xml");
            //}
            saxReader = new SAXReader();

            Document document = saxReader.read(inputStream);
            if (inputStream != null) {
                inputStream.close();
            }
            Element root = document.getRootElement();
            Element blessings = root.element("blessings");
            Iterator iterator = blessings.elementIterator();
            while (iterator.hasNext()) {
                Element blessing = (Element) iterator.next();
                if (blessing.attribute("id").getValue().equals(id + "")) {
                    Element e = blessing.element(element);

                    return e != null ? e.getText() : null;
                }
            }
        } catch (DocumentException | IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    public static String readXMLInformation(String path, String element) {
        //InputStream inputStream = FileUtil.class.getClassLoader().getResourceAsStream(path);
        SAXReader saxReader = null;
        try {
            //if (inputStream == null) {
            //    inputStream = new FileInputStream("./conf/config.xml");
            //}
            InputStream inputStream = new FileInputStream("./conf/config.xml");

            saxReader = new SAXReader();
            Document document = saxReader.read(inputStream);
            Element root = document.getRootElement();
            Element information = root.element("information");
            String ret = information.element(element).getText();
            if (inputStream != null) {
                inputStream.close();
            }
            return ret;
        } catch (DocumentException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void writeXMLLuckyNumber(String path, int number) {
        //String configUrl = Objects.requireNonNull(FileUtil.class.getResource(path)).getPath();
        //URL resource = FileUtil.class.getClassLoader().getResource(path);
        SAXReader saxReader = null;
        try {
            saxReader = new SAXReader();
            //Document document = saxReader.read(new File(configUrl));
            //Document document = saxReader.read(resource);
            Document document = saxReader.read(new File("./conf/config.xml"));
            Element root = document.getRootElement();
            Element luckyNumber = root.element("luckyNumber");
            luckyNumber.setText(String.valueOf(number));
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            //XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(configUrl), outputFormat);
            //XMLWriter xmlWriter = new XMLWriter(new FileWriter(resource.getPath()), outputFormat);
            XMLWriter xmlWriter = new XMLWriter(new FileWriter(path), outputFormat);
            xmlWriter.write(document);
            xmlWriter.close();
        } catch (DocumentException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static int readXMLLuckyNumber(String path) {
        //String configUrl = Objects.requireNonNull(FileUtil.class.getResource(path)).getPath();
        //InputStream inputStream = FileUtil.class.getClassLoader().getResourceAsStream(path);
        //String configUrl = path;
        SAXReader saxReader = null;
        try {
            InputStream inputStream = new FileInputStream("./conf/config.xml");
            saxReader = new SAXReader();
            //Document document = saxReader.read(new File(configUrl));
            Document document = saxReader.read(inputStream);
            Element root = document.getRootElement();
            Element luckyNumber = root.element("luckyNumber");
            return Integer.parseInt(luckyNumber.getText());
        } catch (DocumentException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static String readBinaryFile(File file) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
            reader.close();
            return sb.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void writeBinaryFile(File file, String content) {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
            writer.write(content);
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
